package org.vsg.common.pagination.wrapper;

import org.vsg.common.pagination.keyword.PaginationKeyword;
import org.vsg.common.pagination.sort.SortField;

public class GenericWebSortByPaginationWrapper extends GenericWebPaginationWrapper implements SortFieldPaginationWrapper {

	private SortField sortField;
	
	/**
	 * @return Returns the sortField.
	 */
	public SortField getSortField() {
		return sortField;
	}

	/**
	 * @param sortField The sortField to set.
	 */
	public void setSortField(SortField sortField) {
		this.sortField = sortField;
	}

	public String getFirstPageUrl() {
		String url = super.getFirstPageUrl();
		if (sortField != null) {
			StringBuffer sortByField = new StringBuffer("&");
			sortByField.append(PaginationKeyword.SINGLESORTBY_FIELD);
			sortByField.append("=");
			sortByField.append(sortField.getField()).append(" ").append(sortField.getSortOrder());
			url += sortByField.toString();
		}
		
		return url;
	}
	
	public String getLastPageUrl() {
		String url = super.getLastPageUrl();
		if (sortField != null) {
			StringBuffer sortByField = new StringBuffer("&");
			sortByField.append(PaginationKeyword.SINGLESORTBY_FIELD);
			sortByField.append("=");
			sortByField.append(sortField.getField()).append(" ").append(sortField.getSortOrder());
			url += sortByField.toString();
		}
		return url;		
	}
	
	public String getNextPageUrl() {
		String url = super.getNextPageUrl();
		if (sortField != null) {
			StringBuffer sortByField = new StringBuffer("&");
			sortByField.append(PaginationKeyword.SINGLESORTBY_FIELD);
			sortByField.append("=");
			sortByField.append(sortField.getField()).append(" ").append(sortField.getSortOrder());
			url += sortByField.toString();
		}
		return url;			
	}
	
	public String getPrevPageUrl() {
		String url = super.getPrevPageUrl();
		if (sortField != null) {
			StringBuffer sortByField = new StringBuffer("&");
			sortByField.append(PaginationKeyword.SINGLESORTBY_FIELD);
			sortByField.append("=");
			sortByField.append(sortField.getField()).append(" ").append(sortField.getSortOrder());
			url += sortByField.toString();
		}
		return url;			
	}
	
	
	public String getNumPageUrl(int pageIndex) {
		String url = super.getNumPageUrl(pageIndex);
		if (sortField != null) {
			StringBuffer sortByField = new StringBuffer("&");
			sortByField.append(PaginationKeyword.SINGLESORTBY_FIELD);
			sortByField.append("=");
			sortByField.append(sortField.getField()).append(" ").append(sortField.getSortOrder());
			url += sortByField.toString();
		}
		return url;			
	}
}
